//
//  TTSignInRecordYesViewController.m
//  Estate
//
//  Created by houzhen on 2018/9/5.
//  Copyright © 2018年 ttsx. All rights reserved.
//

#import "TTSignInRecordYesViewController.h"
#import "indexTableCell.h"
#import "QianDViewController.h"
#import "LYEmptyViewHeader.h"

@interface TTSignInRecordYesViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(strong, nonatomic)UITableView *tableView;
@property (strong, nonatomic)NSMutableArray *dataArray;
@end

@implementation TTSignInRecordYesViewController

-(NSMutableArray *)dataArray{
    if(!_dataArray){
        _dataArray=[NSMutableArray array];
    }
    return _dataArray;
}

-(UITableView *)tableView {
    if (_tableView == nil) {
        CGRect rect = CGRectMake(0, 10, self.view.frame.size.width, self.view.frame.size.height);
        _tableView = [[UITableView alloc] initWithFrame:rect];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
        [_tableView registerNib:[UINib nibWithNibName:@"indexTableCell" bundle:nil] forCellReuseIdentifier:@"index"];
        _tableView.tableFooterView = [[UIView alloc]init];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.tableView];
    LYEmptyView *emptyView = [LYEmptyView emptyViewWithImageStr:@"ic_empty"
                              
                                                       titleStr:@"暂无数据"
                              
                                                      detailStr:@""];
    //元素竖直方向的间距
    emptyView.imageSize = CGSizeMake(150, 150);
    emptyView.contentViewY = 120.f;
    self.tableView.ly_emptyView = emptyView;
    [self operateDataBase];
    self.view.backgroundColor = [UIColor whiteColor];
}

//根据项目角色查询人员
- (NSMutableArray *)queryByType:(NSString *)type{
    FMDatabase *db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    NSString *sql=[NSString stringWithFormat:@"SELECT USER_ID FROM PROJECT_USER WHERE PRO_ID='%@' AND USER_TYPE='%@'",PROID,type];
    FMResultSet *result=[db executeQuery:sql];
    NSMutableArray *arrName=[NSMutableArray array];
    while ([result next]) {
        NSString *userid=[result stringForColumn:@"USER_ID"];
        [arrName addObject:userid];
    }
    return arrName;
}


//查出数据
-(void)resultSetSql:(NSString *)sql{
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    FMResultSet *resultSet = [db executeQuery:sql];
    // 遍历结果
    while ([resultSet next]) {
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        
        // 用户id
        NSString *id = [resultSet stringForColumn:@"RECORD_USER"];
        // 类型
        NSString *type = [resultSet stringForColumn:@"RECORD_TYPE"];
        [dic setObject:type forKey:@"type"];
        
        // 用户
        NSString *name = [self userById:id];
        [dic setObject:DEFISNUL(name) forKey:@"name"];
        
        // 拼接字符串
        [dic setObject:[resultSet stringForColumn:@"RECORD_TITLE"] forKey:@"cellText"];
        // 时间
        NSString *time=[resultSet stringForColumn:@"CREATE_TIME"];
        [dic setObject:time forKey:@"time"];
        
        // 附件id
        NSString *fileId = [resultSet stringForColumn:@"RECORD_FILE_PATH"];
        fileId = [LSUtility stringWithString:fileId];
        NSArray *arrs = [LSUtility dictionaryWithJsonString:fileId];
        NSDictionary *fileDic = [arrs firstObject];
        [dic setObject:fileDic[@"fileId"] forKey:@"fileId"];
        
        // 地区 天气
        NSString *addAndWea=[resultSet stringForColumn:@"WEATHER"];
        NSArray *arr=[addAndWea componentsSeparatedByString:@"/"];
        // 天气
        [dic setObject:DEFISNUL(arr[0]) forKey:@"address"];
        
        // 地区
        [dic setObject:DEFISNUL([arr lastObject]) forKey:@"weather"];
        // 记录地点
        NSString *addInfo=[resultSet stringForColumn:@"ADDRES_INFO"];
        [dic setObject:DEFISNUL(addInfo) forKey:@"addInfo"];
        
        // 添加到数组
        [self.dataArray addObject:dic];
    }
}

#pragma mark-数据库操作
- (void)operateDataBase {
    [self.dataArray removeAllObjects];
    // 数据库操作 签到签退查看
    if([USERROLE isEqualToString:@"USER_01"]){
        NSString *sqlStr= [NSString stringWithFormat:@"SELECT * FROM WORK_RECORD WHERE RECORD_TYPE IN('RECORD_05','RECORD_06') AND PRO_ID='%@' ORDER by CREATE_TIME DESC",PROID];
        [self resultSetSql:sqlStr];

    }else{
        NSString *sqlStr=SQL_WORKRECORD_SELECT;
        [self resultSetSql:sqlStr];
        //查询所有监理userid
        NSMutableArray *userids=[self queryByType:@"USER_03"];
        userids = [userids valueForKeyPath:@"@distinctUnionOfObjects.self"];
        for (NSString *userid in userids) {
            NSString *sqlStr = [NSString stringWithFormat:@"SELECT * FROM WORK_RECORD WHERE RECORD_TYPE IN('RECORD_05','RECORD_06') AND PRO_ID='%@' AND RECORD_USER='%@' and STATE='1' ORDER by CREATE_TIME DESC",PROID,userid];
            [self resultSetSql:sqlStr];
        }
        
    }
    
    [self.tableView reloadData];
}

//查询用户
- (NSString *)userById:(NSString *)id
{
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    NSString *sql=[NSString stringWithFormat:@"SELECT * FROM USER WHERE ID='%@'",id];
    FMResultSet *resultSet = [db executeQuery:sql];
    NSString *name;
    while ([resultSet next]) {
        name = [resultSet stringForColumn:@"REAL_NAME"];
    }
    return name;
}

#pragma mark-UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    indexTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"index" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    //设置文字
    NSDictionary *dict = self.dataArray[indexPath.row];
    cell.qiandaoUser.text=dict[@"cellText"];
    
    //判断签到 签退
    if([dict[@"type"] isEqualToString:@"RECORD_05"]){
        cell.line.backgroundColor=[UIColor colorWithRed:0.68 green:0.36 blue:0.63 alpha:1.00];
    }else{
        cell.line.backgroundColor=[UIColor colorWithRed:0.00 green:0.59 blue:0.53 alpha:1.00];
    }
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    //设置时间
    cell.qiandaoDate.text = dict[@"time"];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = self.dataArray[indexPath.row];
    QianDViewController *qiand = [[QianDViewController alloc]init];
    qiand.dict = dict;
    [self.navigationController pushViewController:qiand animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
